local kanluan = fk.CreateSkill{
  name = "lingling__kanluan",
}

Fk:loadTranslationTable{
  ["lingling__kanluan"] = "戡乱",
  [":lingling__kanluan"] = "出牌阶段限一次，你可以与一名其他角色同时重铸一张牌，若皆为红色，你对其造成1点伤害；若皆为黑色，你获得其一张牌；"..
  "若颜色不同，你弃置一张牌、翻面并重置此技能。",

  ["#lingling__kanluan"] = "戡乱：与一名角色同时重铸一张牌，根据颜色执行效果",
  ["#lingling__kanluan-ask"] = "戡乱：请重铸一张牌",
}

kanluan:addEffect("active", {
  anim_type = "offensive",
  prompt = "#lingling__kanluan",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedEffectTimes(kanluan.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and not to_select:isNude()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local result = room:askToJointCards(player, {
      players = {player, target},
      min_num = 1,
      max_num = 1,
      include_equip = true,
      cancelable = false,
      skill_name = kanluan.name,
      prompt = "#lingling__kanluan-ask",
    })
    local moves = {}
    local yes = nil
    for _, p in ipairs({player, target}) do
      local throw = result[p][1]
      if throw then
        if yes == nil then
          yes = Fk:getCardById(throw).color
        else
          yes = yes == Fk:getCardById(throw).color and yes ~= Card.NoColor and yes or nil
        end
        table.insert(moves, {
          ids = {throw},
          from = p,
          toArea = Card.DiscardPile,
          moveReason = fk.ReasonRecast,
          proposer = p,
          skillName = kanluan.name,
        })
        room:sendFootnote({throw}, {
          type = "##RecastCard",
          from = p.id,
        })
        room:broadcastPlaySound("./audio/system/recast")
        room:sendLog{
          type = "#RecastBySkill",
          from = p.id,
          card = {throw},
          arg = kanluan.name,
        }
      end
    end
    room:moveCards(table.unpack(moves))
    if not player.dead then
      player:drawCards(1, kanluan.name)
    end
    if not target.dead then
      target:drawCards(1, kanluan.name)
    end
    if yes then
      if player.dead or target.dead then return end
      if yes == Card.Red then
        room:damage{
          from = player,
          to = target,
          damage = 1,
          skillName = kanluan.name,
        }
      elseif yes == Card.Black then
        if not target:isNude() then
          local card = room:askToChooseCard(player, {
            target = target,
            flag = "he",
            skill_name = kanluan.name,
          })
          room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, kanluan.name, nil, false, player)
        end
      end
    elseif not player.dead then
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = kanluan.name,
        cancelable = false,
      })
      if player.dead then return end
      player:turnOver()
      if player.dead then return end
      player:setSkillUseHistory(kanluan.name, 0, Player.HistoryPhase)
    end
  end,
})

return kanluan
